<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class ShopOrderGoods extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_shop_order_goods';


    protected $append = [

        'refund_num',

        'refund_ing'


    ];




    /**
     * @param $value
     * @param $data
     * @功能说明:是否退款中
     * @author chenniang
     * @DataTime: 2021-04-12 10:46
     */
    public function getRefundIngAttr($value,$data){

        if(!empty($data['id'])){

            $refund_model = new ShopRefund();

            $res = $refund_model->orderRefundIng($data['id']);

            return $res;
        }

    }

    /**
     * @param $value
     * @param $data
     * @功能说明:获取退款的数量
     * @author chenniang
     * @DataTime: 2021-04-12 10:46
     */
    public function getRefundNumAttr($value,$data){

        if(!empty($data['id'])){

            $refund_model = new ShopRefund();

            $num = $refund_model->refundNum($data['id']);

            return $num;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataSelect($dis){

        $data = $this->where($dis)->order('id desc')->select()->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-22 11:12
     * @功能说明:添加商品子订单
     */
    public function orderGoodsAdd($order_goods,$order_id,$user_id,$store_id,$address_id=0){

        $goods_model = new ShopGoods();

        $car_model   = new Car();

        $config_model= new Config();

        foreach ($order_goods as $v){
//            //限购
//            if($v['is_limit']==2){
//                //已购数量
//                $buy_num = $this->getGoodsNumber(['b.user_id'=>$user_id,'a.goods_id'=>$v['goods_id']]);
//
//                if($v['limit']<$buy_num+$v['goods_num']){
//
//                    return ['code'=>500,'msg'=>$v['name'].'超出限购数量,限购数量'.$v['limit']];
//                }
//
//            }
            if(!empty($address_id)){

                $res = $config_model->getGoodsSendPrice($address_id,$v,2);

                if(!empty($res['code'])){

                    return $res;
                }

            }
            //校验
            $insert = [

                'uniacid'        => $v['uniacid'],

                'order_id'       => $order_id,

                'user_id'        => $user_id,

                'pay_type'       => 1,

                'goods_name'     => $v['goods_name'],

                'goods_cover'    => $v['cover'],

                'spe_name'       => $v['spe_name'],

                'goods_price'    => $v['price'],

                'pay_price'      => $v['true_price'],

                'singe_pay_price'=> $v['true_price']/$v['goods_num'],

                'goods_num'      => $v['goods_num'],

                'can_refund_num' => $v['goods_num'],

                'spe_id'         => $v['spe_id'],

                'goods_id'       => $v['goods_id'],

                'integral'       => !empty($v['integral_id'])?$v['integral']:0,

                'i_price'        => !empty($v['integral_id'])?$v['i_price']:0,

                'integral_id'    => $v['integral_id'],

                'kill_atv_id'    => $v['kill_atv_id'],

            ];

            $res = $this->dataAdd($insert);

            if($res!=1){

                return ['code'=>500,'msg'=>'下单失败'];
            }

            //减少库存 增加销量
            $res = $goods_model->setOrDelStock($v['goods_id'],$v['spe_id'],$v['goods_num'],0,0,0,$v['kill_atv_id']);

            if(!empty($res['code'])){

                return $res;
            }
            //删除购物车
            $dis = [

                'user_id' => $user_id,

                'status'  => 1,

                'goods_id'=> $v['goods_id'],

                'spe_id'  => $v['spe_id'],

                'farmer_id' => $store_id
            ];

            $res = $car_model->where($dis)->delete();

        }

        return true;

    }


    /**
     * @param $dis
     * @return float
     * 获取历史商品件数
     */
    public function getGoodsNumber($dis){

        //取消订单的不算 chen
        $num = $this->alias('a')
            ->join('longbing_card_v2_shop_order_list b','a.order_id = b.id')
            ->where($dis)
            ->where('b.pay_type','>=',1)
            ->group('a.id')
            ->sum('a.goods_num');


        $order_id = $this->alias('a')
            ->join('longbing_card_v2_shop_order_list b','a.order_id = b.id')
            ->where($dis)
            ->where('b.pay_type','>=',1)
            ->column('b.id');

        $order_refund_goods = new RefundOrderGoods();
        //申请退款成功的
        $refund_num = $order_refund_goods->where('order_id','in',$order_id)->where(['status'=>2])->sum('goods_num');

        return $num - $refund_num;

    }






}